package com.ruoyi.activity.controller;

import java.util.LinkedHashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.activity.api.ReShopService;
import com.ruoyi.activity.api.domain.CheShop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.activity.domain.CheActivity;
import com.ruoyi.activity.service.ICheActivityService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 信息发布Controller
 * 
 * @author ruoyi
 * @date 2024-06-04
 */
@RestController
@RequestMapping("/activity")
public class CheActivityController extends BaseController
{
    @Autowired
    private ICheActivityService cheActivityService;
    @Autowired
    private ReShopService reShopService;

    /**
     * 查询信息发布列表
     */
//    @RequiresPermissions("activity:activity:list")
    @GetMapping("/list")
    public TableDataInfo list(CheActivity cheActivity)
    {
        startPage();
        List<CheActivity> list = cheActivityService.selectCheActivityList(cheActivity);

        for(CheActivity che : list){
            AjaxResult info = reShopService.getInfo(che.getShopId());
            LinkedHashMap data = (LinkedHashMap) info.get("data");
            CheShop cheShop = BeanUtil.mapToBean(data, CheShop.class, true);
//            System.out.println(cheShop.getBusinessName());
            che.setShopName(cheShop.getBusinessName());
            che.setShopAddress(cheShop.getAddress());
//            System.out.println(cheShop.getAddress());

        }

        return getDataTable(list);
    }

    /**
     * 导出信息发布列表
     */
    @RequiresPermissions("activity:activity:export")
    @Log(title = "信息发布", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CheActivity cheActivity)
    {
        List<CheActivity> list = cheActivityService.selectCheActivityList(cheActivity);
        ExcelUtil<CheActivity> util = new ExcelUtil<CheActivity>(CheActivity.class);
        util.exportExcel(response, list, "信息发布数据");
    }

    /**
     * 获取信息发布详细信息
     */
    @RequiresPermissions("activity:activity:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cheActivityService.selectCheActivityById(id));
    }

    /**
     * 新增信息发布
     */
    @RequiresPermissions("activity:activity:add")
    @Log(title = "信息发布", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CheActivity cheActivity)
    {
        return toAjax(cheActivityService.insertCheActivity(cheActivity));
    }

    /**
     * 修改信息发布
     */
    @RequiresPermissions("activity:activity:edit")
    @Log(title = "信息发布", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CheActivity cheActivity)
    {
        return toAjax(cheActivityService.updateCheActivity(cheActivity));
    }

    /**
     * 删除信息发布
     */
    @RequiresPermissions("activity:activity:remove")
    @Log(title = "信息发布", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cheActivityService.deleteCheActivityByIds(ids));
    }
}
